<?php


if (isset($_GET['action']) && $_GET['action'] == 'manual' && isset($_GET['step']) && $_GET['step'] == '1' && !isset($_POST['subaction'])) {

    $selected_raid = $_GET['raid'];

    $smarty->assign('selected_raid', $selected_raid);

    $previous_raids = $raid->get(0,1, 15);

    $all_players = $player->get(-1,"",-1,0, 0, "name");

    $main_signups = $signup->get($selected_raid, 0, 0);

    foreach($main_signups as $main_player)
    {
        $player_ids[] = $main_player['player_id'];
    }

    $main_id_list = implode(";", $player_ids);

    $smarty->assign('players', $all_players);

    $smarty->assign('step', $_GET['step']);

    $smarty->assign('mains', $main_signups);

    $smarty->assign('main_id_list', $main_id_list);

    $smarty->assign('previous_raids', $previous_raids);

    $smarty->display('manual_select_members.tpl', $_SESSION['uid']);

} elseif (isset($_GET['action']) && $_GET['action'] == 'manual' && isset($_POST['step']) && $_POST['step'] == '1' && isset($_POST['subaction']) && $_POST['subaction'] == 'update_list' && $_POST['submit'] <> "Next Step") {

    $submit_type = $_POST['submit'];

    $player_ids = explode(";", $_POST['main_id_list']);

    if($submit_type == "delete") {

        //do delete stuff

        foreach($_POST['Main'] as $id){

            if($_POST['CheckMain' . $id] <> "on") {
                $temp_ids[] = $id;
            }

        }

        foreach ($temp_ids as $id){

            $current_player = $player->get($id);

            $main_signups[] = $current_player[0];
        }

        $main_id_list = implode(";", $temp_ids);


    } elseif($submit_type == "Add Player") {



        $already_added = false;

        foreach($_POST['Main'] as $id){

            if($id == $_POST['players'])
            {
                $already_added = true;
            }

        }

        if(!$already_added)
        {

            $player_ids[] = $_POST['players'];

        }

        $main_id_list = implode(";", $player_ids);

        foreach ($player_ids as $id){

            $current_player = $player->get($id);

            $main_signups[] = $current_player[0];
        }
    }


    $selected_raid = $_GET['raid'];

    $smarty->assign('selected_raid', $selected_raid);

    $previous_raids = $raid->get(0,1, 15);

    $all_players = $player->get(-1,"",-1,0, 0, "name");

    $smarty->assign('players', $all_players);

    $smarty->assign('step', $_POST['step']);

    $smarty->assign('mains', $main_signups);

    $smarty->assign('main_id_list', $main_id_list);

    $smarty->assign('previous_raids', $previous_raids);

    $smarty->display('manual_select_members.tpl', $_SESSION['uid']);

} elseif (isset($_GET['action']) && $_GET['action'] == 'manual' && isset($_POST['step']) && $_POST['step'] == '1' && isset($_POST['subaction']) && $_POST['subaction'] == 'update_list' && $_POST['submit'] == "Next Step") {

    $selected_raid = $_GET['raid'];

    $smarty->assign('selected_raid', $selected_raid);

    $previous_raids = $raid->get(0,1, 15);

    $smarty->assign('step', 2);

    $smarty->assign('main_id_list', $_POST['main_id_list']);

    $smarty->display('manual_select_events.tpl', $_SESSION['uid']);

} elseif (isset($_GET['action']) && $_GET['action'] == 'manual' && isset($_POST['step']) && $_POST['step'] == '2' && isset($_POST['subaction']) && $_POST['subaction'] == 'update_list' && $_POST['submit'] <> "Next Step") {

    $submit_type = $_POST['submit'];

    $rp_events = array();

    if($submit_type == "delete") {

        if(!empty($_POST['Main'])) {

            foreach ($_POST['Main'] as $id) {

                if($_POST['CheckEvent' . $id] <> "on") {

                    $rp_events[$id]['description'] = $_POST['MainDescription' . $id];
                    $rp_events[$id]['rp'] = $_POST['MainRP' . $id];
                    $rp_events[$id]['id'] = $id;

                }
            }

        }

    } elseif($submit_type == "Add Event") {

        $highest = 0;



        if(!empty($_POST['Main'])) {

            foreach ($_POST['Main'] as $id) {

                $rp_events[$id]['description'] = $_POST['MainDescription' . $id];
                $rp_events[$id]['rp'] = $_POST['MainRP' . $id];
                $rp_events[$id]['id'] = $id;

                $highest = $id;

            }

        }
        $highest = $highest + 1;

        $rp_events[$highest]['description'] = $_POST['description'];
        $rp_events[$highest]['rp'] = $_POST['rp'];
        $rp_events[$highest]['id'] = $highest;

    }


    $selected_raid = $_GET['raid'];

    $smarty->assign('selected_raid', $selected_raid);

    $previous_raids = $raid->get(0,1, 15);

    $smarty->assign('step', 2);

    $smarty->assign('events', $rp_events);

    $smarty->assign('main_id_list', $_POST['main_id_list']);

    $smarty->display('manual_select_events.tpl', $_SESSION['uid']);


}

?>